# 使用归档日志--插入mongo--数据调用redis
import time
from mongo_ import MongoArchive
from redis_ import RedisArchive

redis_client = RedisArchive()
mongo_client = MongoArchive()


class TopType:
    @staticmethod
    def AddAcvs(arr):
        # 建档-acvs_建档插入数据
        arrr = arr[1].split('>')
        acvs_id = arrr[0]  # 档案id
        face_id = arrr[1]
        face_img = redis_client.rdb_get_face(face_id)
        data_dic = {'acvslen1': 0, 'acvslen2': 0, 'acvslen3': 0, 'created_time': int(face_img['timestamp']),
                    'update_time': int(face_img['timestamp']), 'len1': 0, 'len2': 0, 'len3': 0, 'lenacvs': 0,
                    'Ratio1': float(0), 'Ratio2': float(0), 'Ratio3': float(0), 'Ratio12': float(0),
                    'lenall': 0, 'face_image_uri': face_img['face_image_uri'], 'f_id': face_id, 'lencard': 0,
                    'p': None}
        # insert update acvs
        mongo_client.mdb_insert_acvs(acvs_id, data_dic)

        mongo_client.insert_add_img(face_img)

    @staticmethod
    def InsAcvs(arr):
        arrr = arr[1].split('>')
        face_image_id = arrr[0]  # face_id
        acvs_id = arrr[1]  # 档案id
        del_list = redis_client.rdb_face_acvs(face_image_id)
        if acvs_id in del_list:
            return None
        count = int(arrr[2])
        level = int(arrr[2][:-5])
        mongo_client.insert_face_acvs(face_image_id, acvs_id, count, level)

    @staticmethod
    def AcvsAcvs(arr):
        arrr = arr[1].split('>')
        acvssrc = arrr[0]  # acvs1
        acvsfllow = arrr[1]  # acvs2
        del_list = redis_client.rdb_acvs_acvs(acvssrc)
        if acvsfllow in del_list:
            return None
        count = int(arrr[2])
        level = int(arrr[2][:-5])
        mongo_client.acvs_tree(acvssrc, acvsfllow)
        mongo_client.acvsacvs(acvssrc, acvsfllow, count, level)

    @staticmethod
    def Card(arr):
        # card_face_score
        arrr = arr[1].split('>')
        card_id = arrr[0]
        # 判断是否删除
        if not redis_client.rdb_delete_card(card_id):
            return None
        acvs_id = arrr[1]
        level = int(arrr[2][:-5])
        similarity = int(arrr[2]) - int('%s' % level + '00000')
        face_id = arrr[3]
        mongo_client.mdb_acrd(card_id, acvs_id, similarity, level, face_id)

    @staticmethod
    def Cardf(arr):
        arrr = arr[1].split('>')
        card_id = arrr[0]
        # 判断是否删除
        if not redis_client.rdb_delete_card(card_id):
            return None
        face_id = arrr[1]
        card_score = int(float(arrr[2]))
        mongo_client.cardf(card_id, face_id, card_score)


def main():
    gcount = 0
    while True:
        print('RUN3 runing', time.time())
        try:
            # consume data
            cmdline = redis_client.rdb_queue_pop_log()
            gcount += 1
            # db free
            if cmdline is None:
                print(gcount, 'log free!')
                time.sleep(1)
                continue
            # print(cmdline)
            # 插入Mongo一份数据  logBase
            mongo_client.ins_logbase(cmdline)
            arr = cmdline.split('&')
            top_func = getattr(TopType, "%s" % arr[0], None)
            if top_func:
                top_func()
        except Exception as e:
            print(e)
            time.sleep(10)
            continue


if __name__ == "__main__":
    main()
